package com.dick.base.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dick.base.aop.SmartQuery;
import com.dick.base.mapper.BusinessProjectMapper;
import com.dick.base.model.BusinessProject;
import com.dick.base.parameter.ProjectParameter;
import com.dick.base.util.DateUtil;
import com.dick.base.util.IdUtil;
import com.dick.base.util.PageInfo;
import com.dick.base.util.SmartQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 工程service
 */
@Service
public class ProjectService {

    @Autowired
    private BusinessProjectMapper businessProjectMapper;

    /**
     * 工程新增和修改
     * @param parameter
     */
    @Transactional
    public void saveProject(ProjectParameter parameter) {
        BusinessProject project = new BusinessProject();
        project.setId(parameter.getId());
        project.setCustomerId(parameter.getCustomerId());
        project.setName(parameter.getName());
        project.setMemo(parameter.getMemo());
        if (project.getId() == null) {
            project.setId(IdUtil.nextId());
            project.setCreateTime(DateUtil.now());
            businessProjectMapper.insert(project);
        } else {
            project.setUpdateTime(DateUtil.now());
            businessProjectMapper.updateById(project);
        }
    }

    /**
     * 工程分页查询
     * @param project
     * @param pageInfo
     * @return
     */
    @SmartQuery(clazz = BusinessProject.class, likeFields = {"name", "memo"})
    public Page<BusinessProject> projectPage(BusinessProject project, PageInfo pageInfo) {
        return businessProjectMapper.selectPage(SmartQueryUtil.getPage(), SmartQueryUtil.getQueryWrapper());
    }

    /**
     * 工程全部列表
     * @return
     */
    public List<BusinessProject> projectList() {
        return businessProjectMapper.selectList(null);
    }
}
